<?php
if (!defined('ABSPATH')) {
	define('ABSPATH', dirname(dirname(__FILE__)) . '/');
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';

require_once ABSPATH . DRINC . '/dr-themes.php';
require_once ABSPATH . DRINC . '/mygravatar.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';


//通过点击链接，获取问题id
$question_id = isset($_GET['qid']) ? $_GET['qid'] : 0;
$order = isset($_GET['order']) ? $_GET['order'] : 'votes';
$msg_id = isset($_GET['msg_id']) ? $_GET['msg_id'] : 0;
$answer_page = isset($_GET['answer_page']) ? $_GET['answer_page'] : 1;

if ($question_id == 0) {
	header('Location: ' . BASE_HOSTPATH . 'dr-includes/dr-page-question-list.php');
}

//连接数据库，获取在线用户id
$login_id = dr_is_login_on();
if ($login_id)
	$user_id = $login_id;


//当点击问题提交的时候
if (dr_post("submit") == true) {

	$type = dr_post('reply_type');
	$question_id = dr_post('question_id');
	$content = dr_post('reply_content');
	if (strlen($content) < 15) {
		echo "<script language='javascript'>alert('回复长度不够15，请重新输入。');history.back(-1);</script>";
		exit;
	}
	else if (strlen($content) > 200) {
		echo "<script language='javascript'>alert('回复字数不得超过200！');history.back(-1);</script>";
		exit;
	}

	$parent = dr_post('parent_id');
	$parent_id = !empty($parent) ? $parent : 0;

	$posts = array(
		'action' => 'reply_question',
		'question_id' => $question_id,
		'type' => $type,
		'parent_id' => $parent_id,
		'content' => $content,
		'user_id' => $user_id
	);

	$result = dr_server_request('dr-app-questions.php', $posts);
	$result_array = json_decode($result, 1);

	if ($result_array['ret'] != 0) {
		echo "$result";
	}


}

//发送HTTP协议，获得相关的问题以及对应的答案
$posts = array(
	'action' => 'get_question_detail',
	'question_id' => $question_id,
	'login_id' => $login_id,
	'msg_id' => $msg_id,
	'order' => $order,
	'answer_page' => $answer_page
);

$result = dr_server_request('dr-app-questions.php', $posts);

$result_array = json_decode($result, 1);

//将数据转换成html格式后输出
dr_to_html($login_id);
dr_to_html($result_array[0]);
dr_to_html($result_array[1]);
dr_to_html($result_array[2]);

$last = end($result_array[2]);
$last_page = $last['order'];
$last_page = $last_page > 1 ? $last_page : 1;
$totalpage = $last_page + $result_array[3];

$question = $result_array[0];
$question['user_avatar'] = (string) new MyGravatar($question['user_email'], 32);

$smarty->assign("is_login", $login_id);
$smarty->assign('user_id', $user_id);
$smarty->assign('order', $order);
$smarty->assign('answer_page', $answer_page);
$smarty->assign("question", $question);
$smarty->assign("answers", $result_array[1]);
$smarty->assign('pages', $result_array[2]);
$smarty->assign('totalpage', $totalpage);


$content = array();
$content['top_navs'] = get_page_top_navigation();
$content['menus'] = get_page_menus('questions');
$content['tabs'] = $content['answers_tabs'] = get_page_tabs('question', '');
$content['tabs']['title'] = sprintf('<a href="/dr-includes/dr-page-question-answer.php?qid=%d">%s</a>', $result_array[0]['title'], $result_array[0]['title']);

$content['answers_tabs'] = get_page_tabs('question_answers', $order);
$content['answers_tabs']['title'] = sprintf($content['answers_tabs']['title'], $question['answers_num']);
foreach ($content['answers_tabs']['tabs'] as $key => &$ans_tabs) {
	$ans_tabs['link'] = sprintf($ans_tabs['link'], $question['id']);
}

$smarty->assign('content', $content);


$smarty->display('showquestion.tpl');



